﻿using ClassMySQL.Classes;
using System.Data;

namespace Library.Classes
{
    internal static class SendToServer
    {
        private static string database = ServerMySQL.Connection.Database;   // Название базы данных
        private static string sql = "";     // Команда SQL

        #region Работа с клиентом
        internal static DataTable GetClients()
        {
            sql = $"SELECT * FROM {database}.clients";
            return ServerMySQL.GetData(sql);
        }

        /// <summary>
        /// Добавление клиента в БД
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        internal static bool AddClient(Client client)
        {
            string birthday = client.Birthday.ToString("yyyy-MM-dd");
            sql = $"INSERT INTO {database}.clients " +
                $"(last_name, first_name, birthday, phone, email) " +
                $"VALUES ('{client.LastName}', '{client.FirstName}', " +
                $"'{birthday}', '{client.Phone}', '{client.Email}')";
            return OperationResult(sql);
        }

        /// <summary>
        /// Редактирование данных клиента в БД
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        internal static bool EditClient(Client client)
        {
            string birthday = client.Birthday.ToString("yyyy-MM-dd");
            sql = $"UPDATE `{database}`.`clients` SET " +
                $"`last_name` = '{client.LastName}', `first_name` = '{client.FirstName}', " +
                $"`birthday` = '{birthday}', `phone` = '{client.Phone}', `email` = '{client.Email}' " +
                $"WHERE (id = '{client.Id}')";
            return OperationResult(sql);
        }
        #endregion

        #region Работа с автором
        /// <summary>
        /// Добавление автора в БД
        /// </summary>
        /// <param name="author"></param>
        /// <returns></returns>
        internal static bool AddAuthor(Author author)
        {
            sql = $"INSERT INTO {database}.authors " +
                $"(last_name, first_name, patronymic, descript) " +
                $"VALUES ('{author.LastName}', '{author.FirstName}', " +
                $"'{author.Patronymic}', '{author.Description}')";
            return OperationResult(sql);
        }

        /// <summary>
        /// Редактирование данных автора в БД
        /// </summary>
        /// <param name="author"></param>
        /// <returns></returns>
        internal static bool EditAuthor(Author author)
        {
            sql = $"UPDATE `{database}`.`authors` SET " +
               $"`last_name` = '{author.LastName}', `first_name` = '{author.FirstName}', " +
               $"`patronymic` = '{author.Patronymic}', `descript` = '{author.Description}' " +
               $"WHERE (id = '{author.Id}')";
            return OperationResult(sql);
        }
        #endregion

        #region Работа с жанром
        /// <summary>
        /// Добавление нового жанра в БД
        /// </summary>
        /// <param name="genreName"></param>
        /// <returns></returns>
        internal static bool AddGenre(Genre genreName)
        {
            sql = $"INSERT INTO {database}.genre " +
                $"(genre_name) " +
                $"VALUES ('{genreName.GenreName}')";
            return OperationResult(sql);
        }

        /// <summary>
        /// Редактирование жанра в БД
        /// </summary>
        /// <param name="genreName"></param>
        /// <returns></returns>
        internal static bool EditGenre(Genre genreName)
        {
            sql = $"UPDATE `{database}`.`genre` SET " +
               $"`genre_name` = '{genreName.GenreName}' " +
               $"WHERE (id = '{genreName.Id}')";
            return OperationResult(sql);
        }
        #endregion

        /// <summary>
        /// Выполнение операции добавления, обновления и удаления.
        /// Возврат - успешность операции.
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        private static bool OperationResult(string sql)
        {
            if (ServerMySQL.CrudOperation(sql) > 0)
                return true;
            else return false;
        }

        // Изменение статуса клиента
        internal static bool ChangeStatus(int id, bool status)
        {
            int st = 1;
            if (status)
                st = 0;

            sql = $"UPDATE `{database}`.`clients` SET " +
            $"`is_active` = '{st}' WHERE (id = '{id}')";
            return OperationResult(sql);
        }

        internal static DataTable GetAuthor()
        {
            sql = $"SELECT * FROM {database}.authors";
            return ServerMySQL.GetData(sql);
        }
    }
}